package com.dao;

import com.bean.Book;
import com.bean.BookPage;
import com.bean.UserPage;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName BookDao
 * @Desc 图书数据控制层
 * @Author xiaoding
 * @Date 2021/1/11 11:19
 * @Version 1.0
 */

public class BookDao {

    /*
     * @Author bookSelect
     * @Description 查询所有图书
     * @Date 11:22 2021/1/11
     * @Param bookPage
     * @return java.util.List<com.bean.Book>
     */
    public List<Book> bookSelect(BookPage bookPage) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.bookSelect";
        List<Book> list = session.selectList(sql,bookPage);
        return list;
    }

    /*
     * @Author bookSelectId，查询图书信息
     * @Description 根据图书Id
     * @Date 9:33 2021/1/17
     * @Param bId
     * @return com.bean.Book
     */
    public Book bookSelectId(int bId) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.bookSelectId";
        Book book = session.selectOne(sql,bId);
        return  book;
    }

    /*
     * @Author bookSearch
     * @Description 根据图书名称搜索图书
     * @Date 16:57 2021/1/13
     * @Param bookPage
     * @return java.util.List<com.bean.Book>
     */
    public List<Book> bookSearch(BookPage bookPage) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.bookSearch";
        List<Book> list = session.selectList(sql,bookPage);
        return list;
    }

    /*
     * @Author bookCount
     * @Description 查询所有图书数量
     * @Date 17:03 2021/1/13
     * @Param
     * @return int
     */
    public int bookCount() {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.bookCount";
        int a = session.selectOne(sql);
        return a;
    }

    /*
     * @Author bookSearchCount
     * @Description 根据图书名称模糊搜索查询
     * @Date 18:30 2021/1/13
     * @Param name
     * @return int
     */
    public int bookSearchCount(String name) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.bookSearchCount";
        int a = session.selectOne(sql,name);
        return a;
    }

    /*
     * @Author addBook
     * @Description 添加图书
     * @Date 18:31 2021/1/13
     * @Param book
     * @return int
     */
    public int addBook(Book book) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.addBook";
        int a = session.insert(sql,book);
        session.commit();
        return a;
    }

    /*
     * @Author updateBook
     * @Description 修改图书信息
     * @Date 18:33 2021/1/13
     * @Param book
     * @return int
     */
    public int updateBook(Book book) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.updateBook";
        int a = session.update(sql,book);
        System.out.println(book);
        session.commit();
        return a;
    }

    /*
     * @Author deleteBook
     * @Description 删除图书
     * @Date 18:35 2021/1/13
     * @Param bId
     * @return int
     */
    public int deleteBook(int bId) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.BookMapping.deleteBook";
        int a = session.delete(sql,bId);
        session.commit();
        return a;
    }
}
